package fr.insta.reverse.uml.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import fr.insta.reverse.uml.bean.Guide;
import fr.insta.reverse.uml.bean.User;
import fr.insta.reverse.uml.routing.Route;
import fr.insta.reverse.uml.service.IService;
import fr.insta.reverse.uml.service.ServiceFactory;
import fr.insta.reverse.uml.service.ServiceGuide;
import fr.insta.reverse.uml.service.ServiceName;
import fr.insta.reverse.uml.util.IhmValues;

public class GuideController extends DefaultController
{
	IService<Guide> service = ServiceFactory.getService(ServiceName.ServiceGuide);

	public GuideController(HttpServletRequest _request, HttpServletResponse _response)
	{
		super(_request, _response);
	}

	@Override
	public void execute()
	{
		String route = (String) request.getAttribute("route");

		if (route.contentEquals(Route.GuideCreate.getRoute()))
		{
			guideCreate();
		}
		else if (route.contentEquals(Route.GuideDelete.getRoute()))
		{
			guideDelete();
		}
		else if (route.contentEquals(Route.GuideRate.getRoute()))
		{
			guideRate();
		}

	}

	private void guideRate()
	{
		if (request.getMethod() == "POST")
		{

			Guide cache = new Guide();
			//			cache.set(((User) request.getSession().getAttribute("User")).getId());
			//			cache.setId(Integer.parseInt(request.getParameter(IhmValues.FORM_GUIDERATE_IDGUIDE)));
			//			cache.setRatedValue(request.getParameter(IhmValues.FORM_GUIDERATE_RATE));
			cache = service.insert(cache);
			if (cache != null)
			{
				//redirect(Route.GuideRate.getRoute());
			}
		}
		else
		{
			// GET ****************
			//redirect(Route.GuideRate.getRoute());
		}
	}

	private void guideDelete()
	{
		if (request.getMethod() == "POST")
		{

			Guide cache = new Guide();
			cache.setId(Integer.parseInt(request.getParameter(IhmValues.FORM_GUIDEDELETE_IDGUIDE)));
			((ServiceGuide) service).delete(cache);
			//redirect(Route.GuideDelete.getRoute());
		}
		else
		{
			// GET ****************
			//redirect(Route.GuideDelete.getRoute());
		}

	}

	private void guideCreate()
	{

		if (request.getMethod() == "POST")
		{
			Guide cache = new Guide();
			cache.setAuteur(((User) request.getSession().getAttribute("User")).getId());
			cache.setLib(request.getParameter(IhmValues.FORM_GUIDECREATE_LABELGUIDE));
			cache.setLien(request.getParameter(IhmValues.FORM_GUIDECREATE_LIENGUIDE));
			cache.setProj(Integer.parseInt(request.getParameter(IhmValues.FORM_GUIDECREATE_IDPROJET)));
			cache = service.insert(cache);
			if (cache != null)
			{
				//redirect(Route.GuideRate.getRoute());
			}
		}
		else
		{
			// GET ****************
			//redirect(Route.GuideRate.getRoute());
		}
	}
}
